|
|
|
|
|
|
|
After you have declared the parameters, you simply place them in the manipulative part of the statement where you want to be able to substitute a value. The following code line shows how a parameter would be used in place of a state ID in a SQL statement: |
|
|
|
|
|
|
|
|
PARAMETERS StateName String; SELECT * FROM Customers
WHERE State = StateName |
|
|
|
|
|
|
|
|
When you go to run the SQL statement in your program, each parameter is treated like a property of the QueryDef. Therefore, you need to assign a value to each parameter before you use the Execute method. The following code shows you how to set the property value for the preceding SQL statement and open a recordset: |
|
|
|
|
|
|
|
|
Dim OldDb As Database, Qry As QueryDef, Rset As Recordset
Set OldDb = DBEngine.Workspaces(0).OpenDatabase("C:\Triton.Mdb")
Set Qry = OldDb.QueryDefs("StateSelect")
Qry!StateName = "AL"
Set Rset = Qry.OpenRecordset() |
|
|
|
|
|
|
|
|
As you can see, using parameters makes it easy to store your queries in the database and still maintain the flexibility of being able to specify comparison values at runtime. |
|
|
|
|
|
|
|
|
In the previous section, you saw how the SELECT statement can be used to retrieve records and place the information in a dynaset or table for further processing by a program. This was just one of the four manipulative statements that you defined earlier in this chapter. The three remaining statements are as follows: |
|
|
|
|
|
|
|
|
DELETE FROMAn action query that removes records from a table |
|
|
|
|
|
|
|
|
INSERT INTOAn action query that adds a group of records to a table |
|
|
|
|
|
|
|
|
UPDATEAn action query that sets the values of fields in a table |
|
|
|
|
|
|
|
|
In the following sections, you take a look at how to use these statements to further refine that data that you are manipulating in a database via a SQL function. |
|
|
|
|
|
|
|
|
Using the DELETE Statement |
|
|
|
|
|
|
|
|
The DELETE statement is used to create an action query. The DELETE statement's purpose is to delete specific records from a table. An action query does not return a group of records into a dynaset as SELECT queries do. Instead, action queries work like program subroutines. That is, an action query performs its functions and returns to the next statement in the calling program. |
|
|
|
|
|
|
|
|
The syntax of the DELETE statement is as follows: |
|
|
|
|
|
|
|
|
DELETE FROM tablename [WHERE clause] |
|
|
|
|
|
|
|
|
The WHERE clause is an optional parameter. If it is omitted, all the records in the target table are deleted. You can use the WHERE clause to limit the deletions to only those records that meet |
|
|
|
|
|